<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="800px"
    @close="close"
  >
    <el-form ref="form" label-width="90px" :model="form" :rules="rules">
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model.trim="form.projectName" :readonly="true" />
      </el-form-item>
      <el-form-item label="项目描述" prop="projectDescription">
        <el-input
          v-model.trim="form.projectDescription"
          :autosize="{ minRows: 4, maxRows: 6 }"
          maxlength="200"
          placeholder="请输入内容"
          type="textarea"
        />
      </el-form-item>
      <el-form-item label="保护状态" prop="isProtect">
        <el-col :span="2">
          <el-tooltip
            :content="form.isProtect === true ? '启用' : '禁用'"
            placement="top"
          >
            <el-switch
              v-model="form.isProtect"
              active-color="#13ce66"
              :active-value="true"
              inactive-color="#ff4949"
              :inactive-value="false"
            />
          </el-tooltip>
        </el-col>
        <el-col class="line" :span="1"></el-col>
        <el-col :span="3">
          <el-link type="primary" :underline="false" @click="resetSecretKey">
            重置秘钥
          </el-link>
        </el-col>
      </el-form-item>

      <el-form-item v-if="form.isProtect" label="SecretKey" prop="secretKey">
        <el-input v-model.trim="form.secretKey" :readonly="true" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import {
    EditProjectInfo,
    ResetSecretKey,
  } from '@/api/projectConfig/projectInfo'

  export default {
    name: 'EditProjectInfoForm',
    data() {
      return {
        form: {
          id: '',
          projectName: '',
          projectDescription: '',
          isProtect: false,
          secretKey: '',
        },
        rules: {
          projectName: [
            { required: true, trigger: 'blur', message: '请输入项目名称' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    methods: {
      showForm(row) {
        this.title = '编辑'
        this.form = {
          id: row.projectID,
          projectName: row.projectName,
          projectDescription: row.projectDescription,
          isProtect: row.isProtect,
          secretKey: row.secretKey,
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (!valid) {
            return
          }

          EditProjectInfo(this.form).then((res) => {
            if (res.data) {
              this.$baseMessage(res.msg, 'success')
              this.$emit('fetch-data')
              this.close()
            } else {
              this.$baseMessage(res.msg, 'error')
            }
          })
        })
      },
      // 重置秘钥
      resetSecretKey() {
        this.$baseConfirm(
          '你确定重置秘钥吗？这会导致业务系统获取配置信息失败！',
          null,
          async () => {
            ResetSecretKey({ ID: this.form.id }).then((res) => {
              if (res.data) {
                this.$baseMessage(res.msg, 'success')
                this.$emit('fetch-data')
                this.close()
              } else {
                this.$baseMessage(res.msg, 'error')
              }
            })
          }
        )
      },
    },
  }
</script>
